const express = require('express')
const cors = require('cors')
const history = require('connect-history-api-fallback') // 处理刷新页面404问题的中间件

// 创建express实例
const app = express()
// 使用中间件处理页面刷新404问题
app.use(history())

// cors中间件配置
const corsOptions = {
  origin: 'http://localhost:5173', // 允许的源
  methods: ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'], // 允许的方法
  allowedHeaders: ['token'], // 允许的自定义头
  optionsSuccessStatus: 200 // 预检请求成功的状态码
}
app.use(cors(corsOptions)); // 使用cors中间件

const students = [
	{ name: '张三丰', id: 1 },
	{ name: '张无忌', id: 2 },
	{ name: '张翠山', id: 3 },
]

// 处理实际请求
app.get('/students', (req, res) => {
  // res.send(students)
  res.json({
    success: true,
    code: 200,
    data: students,
    message: '获取用户列表成功'
  });
})

const routes = [
      {
        path: '/home',
        name: 'home',
        component: './views/home/index.vue',
        meta: {
          title: '首页'
        }
      },
      {
        path: '/customer',
        name: 'customer',
        meta: {
          title: '会员'
        },
        children: [
          {
            path: '/customer/list',
            name: 'customerList',
            component: './views/customer/list.vue',
            meta: {
              title: '会员列表'
            }
          },
          {
            path: '/customer/group',
            name: 'customerGroup',
            component: './views/customer/group.vue',
            meta: {
              title: '会员分组'
            }
          }
        ]
      },
      {
        path: '/menus',
        name: 'firstMenu',
        meta: {
          title: '一级菜单'
        },
        children: [
          {
            path: '/menus/second',
            name: 'SecondMenu',
            meta: {
              title: '二级菜单'
            },
            children: [
              {
                path: '/menus/third1',
                name: 'ThirdMenu1',
                component: './views/menuDemo/thirdMenu1.vue',
                meta: {
                  title: '三级菜单1'
                }
              },
              {
                path: '/menus/third2',
                name: 'ThirdMenu2',
                component: './views/menuDemo/thirdMenu2.vue',
                meta: {
                  title: '三级菜单2'
                }
              },
              {
                path: '/menus/third3',
                name: 'ThirdMenu3',
                component: './views/menuDemo/thirdMenu3.vue',
                meta: {
                  title: '三级菜单3'
                }
              }
            ]
          }
        ]
      },
      {
        path: "/log",
        name: "Log",
        component: './views/log/index.vue',
        meta: {
          title: '日志管理'
        }
      }
]
app.get('/routes', (req, res) => {
  // res.send(routes)
  res.json({
    success: true,
    code: 200,
    data: routes,
    message: '获取路由列表成功'
  });
})
// 指定静态文件
app.use(express.static(__dirname + '/dist'))

// 开启本地服务器
app.listen('8099', () => {
  console.log('初始化一个node本地服务器2，http://127.0.0.1:8099')
})

